﻿using System.Drawing;
using System.Windows.Forms;
using GeoSketch2.Core;

namespace GeoSketch2.Archaeo
{
    /// <summary> 
    /// The tool to draw Archaeo freehand
    /// </summary>
    public class TFreeHand:BaseArchaeoTool
    {
        #region Fields
        /// <summary> 
        /// The current polyline
        /// </summary>
        public Polyline CurrentPolyline;
        /// <summary> 
        /// The current type of vertex
        /// </summary>
        VertexType _CurrentVertexType;

        //Indicates that the mouse button is pressed
        bool _MouseDown;

        Point PreviousPoint;
        #endregion

        #region Constructor
        /// <summary> 
        /// The tool to draw Archaeo freehand
        /// </summary>
        /// <param name="Viewport">The viewport to draw to</param>
        public TFreeHand(Viewport Viewport)
            : base(Viewport,"Free Hand")
        {
            _CurrentVertexType = VertexType.Line;
        } 
        #endregion

        #region Inputs
        /// <summary>
        /// The method called when the mouse button is down
        /// </summary>
        public override void MouseDown(MouseEventArgs MouseArgs)
        {
            _MouseDown = true;
            NewPolyline();
            PreviousPoint = MouseArgs.Location;
            CurrentPolyline.Vertices.Add(new Vertex(Viewport.Object2ImageTransformation.InverseTransform(PreviousPoint), _CurrentVertexType));
        }

        /// <summary>
        /// The method called when the mouse button is up
        /// </summary>
        public override void MouseUp(MouseEventArgs MouseArgs)
        {
            _MouseDown = false;
            Viewport.GeoObjects.Add(CurrentPolyline);
            RefreshViewport();
        }

        /// <summary>
        /// The method called when the mouse button is moved
        /// </summary>
        public override void MouseMove(MouseEventArgs MouseArgs)
        {
            if (_MouseDown)
            {
                Point CurrentPoint = MouseArgs.Location;
                if (CurrentPoint.X > PreviousPoint.X + Vars.Setup.Tools.TFreeHand.SamplingRate ||
                    CurrentPoint.X < PreviousPoint.X - Vars.Setup.Tools.TFreeHand.SamplingRate ||
                    CurrentPoint.Y > PreviousPoint.Y + Vars.Setup.Tools.TFreeHand.SamplingRate ||
                    CurrentPoint.Y < PreviousPoint.Y - Vars.Setup.Tools.TFreeHand.SamplingRate)
                {
                    PreviousPoint = CurrentPoint;
                    CurrentPolyline.Vertices.Add(new Vertex(Viewport.Object2ImageTransformation.InverseTransform(PreviousPoint), _CurrentVertexType));
                    CurrentPolyline.Draw(Viewport);
                }
            }
        } 
        #endregion

        #region Methods
        /// <summary>
        /// Creates a new polyline
        /// </summary>
        public void NewPolyline()
        {
            CurrentPolyline = new Polyline(null,Viewport.CurrentLayer, Viewport.CurrentExcavationArea, Viewport.CurrentLineType,Viewport.CurrentColor);
        }

        /// <summary>
        /// The method to be used to send the first message to the user, just after beeing selected
        /// </summary>
        public override void SendFirstMessage()
        {
            SendMessage("Draw");
        }
        #endregion
    }
}
